﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Practice___组合单选框绑定.Model;

namespace Practice___组合单选框绑定.ViewModel
{
    public class CheckBoxViewModel:ViewModelBase
    {
        public CheckBoxViewModel()
        {
            checkboxs = new List<CheckBoxModel>()
            {
                new CheckBoxModel(){Content = "c#"},
                new CheckBoxModel(){Content = "cpp"},
                new CheckBoxModel(){Content = "python"},
                new CheckBoxModel(){Content = "dart"},
                new CheckBoxModel(){Content = "flutter"},
                new CheckBoxModel(){Content = "wpf"},
                new CheckBoxModel(){Content = "mvvm"}
            };
        }

        private string checkedStr;

        public string CheckedStr
        {
            get { return checkedStr; }
            set
            {
                checkedStr = value;
                RaisePropertyChanged(nameof(CheckedStr));
            }
        }

        private List<CheckBoxModel> checkboxs;

		public List<CheckBoxModel> CheckBoxs
		{
			get => checkboxs;
            set
            {
                checkboxs = value;
                RaisePropertyChanged(nameof(CheckBoxs));
            }
        }

        private RelayCommand checkBoxCommand;

        public RelayCommand CheckBoxCommand
        {
            get => checkBoxCommand ?? (checkBoxCommand = new RelayCommand(ExecuteChecked));
            set => checkBoxCommand = value;
        }

        private void ExecuteChecked()
        {
            CheckedStr = "";
            if (CheckBoxs != null && CheckBoxs.Count > 0)
            {
                var list = CheckBoxs.Where(p => p.IsChecked);
                foreach (var model in list)
                {
                    CheckedStr += model.Content + ",";
                }

                // 去掉尾部的','
                CheckedStr = CheckedStr.TrimEnd(new[] {','});
            }
        }
    }
}
